/**
 * This class provides the functionality to filter out villages from the view
 * those are outside the travel time from a specified target village.
 */
function TtwTroopFilter() {
    /**
     * Build the filter form.
     */
    this.buildForm = function() {
        var filterFormSrc = '<form id="filterForm"><input type="text" id="filterTarget" name="filterTarget" size="5"/>&nbsp;<select id="filterUnit" name="filterUnit">';

        for (var i = 0; i < ttw.units.length; i++) {
            filterFormSrc += '<option value="' + i + '">' + ttw.units[i].name + '</option>';
        }

        filterFormSrc += '</select>&nbsp;<input type="text" id="filterHours" name="filterHours" value="1" size="3"/>&nbsp;<input type="text" id="filterMinutes" name="filterMinutes" value="0" size="3"/>&nbsp;<input type="button" id="filterButton" name="filterButton" value="Filter" onclick="javascript:ttwTroopFilter.doFilter();"/></form>';

        $('#units_table').before(filterFormSrc);
    }

    /**
     * Validate filter parameters.
     *
     * @param target text that contains target coordinates
     * @param unitId id of the unit within @see ttw.units
     * @param hours number of hours of the maximum travel time
     * @param minutes number of minutes of the maximum travle time
     *      (shoul be below 60).
     * @return an error message or null
     */
    this.validate = function(target, unitId, hours, minutes) {
        if (!target.match(/^\d{1,3}\|\d{1,3}$/)) return 'Invalid target!';
        if (!hours.match(/^\d{1,3}$/)) return 'Invalid hours!';
        if (!minutes.match(/^\d{1,2}$/) || parseInt(minutes) > 59) return 'Invalid minutes!';
    };
    
    /**
     * Apply the filter with the given parameters.
     *
     * @param target text that contains target coordinates
     * @param unitId id of the unit within @see ttw.units
     * @param hours number of hours of the maximum travel time
     * @param minutes number of minutes of the maximum travle time
     *      (shoul be below 60).
     */
    this.applyFilter = function(target, unitId, hours, minutes) {
        $('#units_table tr:gt(0)').each(function() {
            var source = $.trim($(this).find('td:eq(0)').text());
            var travelTime = ttw.getTravelTime(source, target, ttw.units[unitId]);
            var maxTime = 60 * hours + minutes;
            
            if (travelTime > maxTime) {
                $(this).hide();
            } else {
                $(this).show();
            }
        });
    };
    
    /**
     * Perform filtering after validation.
     */
    this.doFilter = function() {
        var target = $('#filterTarget').val();
        var unitId = $('#filterUnit').val();
        var hours = $('#filterHours').val();
        var minutes = $('#filterMinutes').val();
        
        var error = this.validate(target, unitId, hours, minutes);
        if (error) {
            alert(error);
        } else {
            this.applyFilter(target, unitId, parseInt(hours), parseInt(minutes));
        }
    }
}

var ttwTroopFilter = new TtwTroopFilter();
